package models

import (
	_ "time"

	orm "go-papers/database"
)

type OrganTeacher struct {
	TeacherId        int    `json:"teacherId" gorm:"type:int(11) unsigned;primary_key"` //
	TeacherName      string `json:"teacherName" gorm:"type:varchar(32);"`               // 教师姓名
	TreacherRegister string `json:"treacherRegister" gorm:"type:varchar(255);"`         // 入职时间
	OrganId          int    `json:"organId" gorm:"type:int(11);"`                       // 机构
	OrganName        string `json:"organName" gorm:"type:varchar(255);"`                // 机构名称
	BrandName        string `json:"brandName" gorm:"type:varchar(255);"`                // 机构品牌名称
	WorkInfo         string `json:"workInfo" gorm:"type:text;"`                         // 工作经历
	TeacherQualify   string `json:"teacherQualify" gorm:"type:text;"`                   // 教师资格证书 图片
	WorkBg           string `json:"workBg" gorm:"type:text;"`                           // 工作背景及业绩
	WorkVerify       string `json:"workVerify" gorm:"type:varchar(255);"`               // 证明材料 图片

	ApprovalStatus string `json:"approvalStatus" gorm:"type:varchar(255);"` // 核准状态 1 待审核  3 失败 5 已通过

	CreateBy     string `json:"createBy" gorm:"type:varchar(255);"`     //
	UpdateBy     string `json:"updateBy" gorm:"type:varchar(255);"`     //
	CreateByName string `json:"createByName" gorm:"type:varchar(255);"` // 创建人
	UpdateByName string `json:"updateByName" gorm:"type:varchar(255);"` // 更新人

	UpdateStart string `json:"updateStart" gorm:"-"` // 跟新 起始时间
	UpdateEnd   string `json:"updateEnd" gorm:"-"`   // 跟新 结束时间

	ApprovalAt       string `json:"approvalAt" gorm:"type:varchar(255);"`       // 核准时间
	ApprovalUserId   string `json:"approvalUserId" gorm:"type:varchar(255);"`   // 核准人id
	ApprovalUserName string `json:"approvalUserName" gorm:"type:varchar(255);"` // 核准人名称
	DataScope        string `json:"dataScope" gorm:"-"`
	Params           string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrganTeacher) TableName() string {
	return "organ_teacher"
}

// 创建OrganTeacher
func (e *OrganTeacher) Create() (OrganTeacher, error) {
	var doc OrganTeacher
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取OrganTeacher
func (e *OrganTeacher) Get() (OrganTeacher, error) {
	var doc OrganTeacher
	table := orm.Eloquent.Table(e.TableName())
	if e.TeacherId != 0 {
		table = table.Where("teacher_id = ?", e.TeacherId)
	}
	if e.TeacherName != "" {
		table = table.Where("teacher_name like ?", "%"+e.TeacherName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("organ_name like ?", "%"+e.OrganName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.WorkInfo != "" {
		table = table.Where("work_info = ?", e.WorkInfo)
	}
	if e.CreateBy != "" {
		table = table.Where("create_by = ?", e.CreateBy)
	}
	if e.UpdateBy != "" {
		table = table.Where("update_by = ?", e.UpdateBy)
	}
	if e.ApprovalStatus != "" {
		table = table.Where("approval_status = ?", e.ApprovalStatus)
	}
	if e.ApprovalAt != "" {
		table = table.Where("approval_at = ?", e.ApprovalAt)
	}
	if e.ApprovalUserId != "" {
		table = table.Where("approval_user_id = ?", e.ApprovalUserId)
	}
	if e.ApprovalUserName != "" {
		table = table.Where("approval_user_name like ?", "%"+e.ApprovalUserName+"%")
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrganTeacher带分页
func (e *OrganTeacher) GetPage(pageSize int, pageIndex int) ([]OrganTeacher, int, error) {
	var doc []OrganTeacher

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.TeacherId != 0 {
		table = table.Where("teacher_id = ?", e.TeacherId)
	}
	if e.TeacherName != "" {
		table = table.Where("teacher_name like ?", "%"+e.TeacherName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("organ_name like ?", "%"+e.OrganName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.WorkInfo != "" {
		table = table.Where("work_info = ?", e.WorkInfo)
	}
	if e.CreateBy != "" {
		table = table.Where("create_by = ?", e.CreateBy)
	}
	if e.UpdateBy != "" {
		table = table.Where("update_by = ?", e.UpdateBy)
	}
	if e.ApprovalStatus != "" {
		table = table.Where("approval_status = ?", e.ApprovalStatus)
	}
	if e.ApprovalAt != "" {
		table = table.Where("approval_at = ?", e.ApprovalAt)
	}
	if e.ApprovalUserId != "" {
		table = table.Where("approval_user_id = ?", e.ApprovalUserId)
	}
	if e.ApprovalUserName != "" {
		table = table.Where("approval_user_name like ?", "%"+e.ApprovalUserName+"%")
	}

	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "organ_teacher", e.DataScope, "机构教师列表")

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrganTeacher
func (e *OrganTeacher) Update(id int) (update OrganTeacher, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("teacher_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrganTeacher
func (e *OrganTeacher) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("teacher_id = ?", id).Delete(&OrganTeacher{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrganTeacher) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("teacher_id in (?)", id).Delete(&OrganTeacher{}).Error; err != nil {
		return
	}
	Result = true
	return
}
